package com.hdaccp.gymxmjpa.Controller;

import com.hdaccp.gymxmjpa.Dao.CardsinfoDao;
import com.hdaccp.gymxmjpa.Dao.SubjectDao;
import com.hdaccp.gymxmjpa.Service.CardsinfoDaoImpl;
import com.hdaccp.gymxmjpa.Service.SubjectDaoImpl;
import com.hdaccp.gymxmjpa.entity.Cardsinfo;
import com.hdaccp.gymxmjpa.entity.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/subject")
public class SubjectController {
   @Autowired
    private SubjectDaoImpl subjectDaoImpl;
   @Autowired
   private SubjectDao subjectDao;

    @RequestMapping("/jin7")
    public String jin7(){

        return "WEB-INF/jsp/subject";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> query(String subname, int pageSize, int pageNumber){
        Map<String,Object>  map1=new HashMap<String,Object>();
        map1.put("subname",subname);
        map1.put("qi",(pageNumber-1)*pageSize);
        map1.put("shi",pageSize);
        return subjectDaoImpl.query(map1);
    }
 @RequestMapping("/del")
    @ResponseBody
    public  Map<String,Object> del(long subId,String subname, int pageSize, int pageNumber){
     subjectDao.deleteById(subId);
     Map<String,Object>  map1=new HashMap<String,Object>();
     map1.put("subname",subname);
     map1.put("qi",(pageNumber-1)*pageSize);
     map1.put("shi",pageSize);
     return subjectDaoImpl.query(map1);
    }
    @RequestMapping("/add")
    @ResponseBody
    public  void save(Subject subject){
        subjectDao.save(subject);
    }
    @RequestMapping("/cha")
    @ResponseBody
    public Optional<Subject> one(long subId){
        return subjectDao.findById(subId);
    }
    @RequestMapping("/upd")
    @ResponseBody
    public  void upd(Subject subject){
        subjectDao.save(subject);
    }
    @RequestMapping("/count")
    @ResponseBody
    public Long count (String subname){
        subjectDaoImpl.count(subname);
        return  subjectDaoImpl.count(subname);
    }

}
